<template>
    <div>
        <!--新增按钮位置-->
        <div style="position:fixed;z-index:100;width:100%">
            <div role="button" tabindex="0" @click="addOne"
                class="van-cell van-cell--clickable van-cell--center van-cell--borderless van-contact-card van-contact-card--add"
                style="position:fixed;width:50%;">
                <div class="van-cell__value van-cell__value--alone van-contact-card__value">添加演示视频</div><i
                    class="van-icon van-icon-arrow van-cell__right-icon">
                </i>
            </div>
            <div style="position:fixed;width:50%;left:50%;padding: 2px;" class="van-cell--borderless van-contact-card">
                <van-dropdown-menu title="班级选择">
                    <van-dropdown-item v-model="classId" :options="classInfos" @change="changeClass" />
                </van-dropdown-menu>
            </div>
        </div>
        <div v-show="show == 0">
            <div style="height: 73px"></div>
            <div role="button" tabindex="0" index="0" v-for="(item, index) in videos" :item="item"
                @click="intoVideoPlay(item)"
                class="van-cell van-cell--clickable van-cell--center van-cell--borderless van-contact-card van-contact-card--edit">
                <i class="van-icon van-icon-contact van-cell__left-icon" />
                <div class="van-cell__value van-cell__value--alone van-contact-card__value">
                    <div>视频名称:{{ item.videoName }}</div>
                    <div>视频描述:{{ item.content }}</div>
                </div>
                <i class="van-icon van-icon-arrow van-cell__right-icon" />
            </div>
            <div style="height: 73px"></div>
        </div>
        <div v-show="show == 1">
            <div style="height: 80px"></div>
            <video :src="videoUrl" width="100%" controls></video>
            <div style="margin: 16px;">
                <van-button round block type="primary" @click="back">返回列表</van-button>
            </div>
        </div>
        <div v-show="show == 2">
            <div style="height: 80px"></div>
            <van-cell-group>
                <van-field v-model="videoName" label="视频名称" left-icon="smile-o" right-icon="warning-o" placeholder="视频名称" />
                <van-field v-model="content" label="视频描述" left-icon="music-o" placeholder="视频描述" />
            </van-cell-group>
            <van-cell-group>
                <van-uploader :max-size="100 * 1024 * 1024" accept=".mp4" v-model="fileList" :after-read="afterRead" />
            </van-cell-group>
            
            <div style="margin: 16px;">
                <van-button round block type="primary" @click="back">返回列表</van-button>
            </div>
        </div>
    </div>
</template>
<style>
.van-dropdown-menu__bar {
    height: 67px;
}
</style>
<script>
import req from '../../request/index'
import { UploadFile } from '../../request/FileHttpRequestUtil'
export default {
    name: '',
    data() {
        return {
            show: 0,
            classInfos: [],
            classId: '',

            fileList: [],

            videos: [],
            videoUrl: '',
            videoName:'',
            content:''
        };
    },
    methods: {
        afterRead(file) {
            var that = this;
            console.log(file.file)
            file.status = 'uploading';
            file.message = '上传中...';
            var data = new FormData();
            data.append("file", file.file);
            data.append("videoName", this.videoName);
            data.append("content", this.content);
            data.append("relationGroupId", this.$store.state.user.id);
            data.append("status", this.classId);

            UploadFile("/api/addVideo", data,function(e){
                if(e.data.code == 0){
                    file.status = 'success';
                    file.message = '上传成功';
                    that.$dialog.alert({title:"上传成功"})
                }
                setTimeout(() => {
                    that.back();
                }, 300);
            })
        },
        back() {
            this.show = 0;
            this.videoUrl = '';
            this.getVideos();
        },
        intoVideoPlay(item) {
            this.show = 1;
            if (item.videoUrl != null) {
                this.videoUrl = "/api/getVideo?url=" + item.videoUrl;
            }
        },
        getVideos() {
            req({
                url: "getVideos",
                method: "post",
                data: {
                    status: this.classId,
                    relationGroupId: this.$store.state.user.id
                }
            }).then(res => {
                if (res.data.code == 0) {
                    this.videos = res.data.data
                }
            }).catch(err => {
                console.log(err);
            })
        },
        back() {
            this.show = 0;
        },
        addOne() {
            this.show = 2;
        },
        getShixun() {
            req({
                url: "getShixun",
                method: "post",
                data: {
                    id: this.$store.state.user.id
                }
            }).then(res => {
                if (res.data.code == 0) {
                    var data = res.data.data;
                    this.classId = res.data.data[0].id;
                    this.practiceTitle = res.data.data[0].practiceTitle;
                    for (var i = 0; i < data.length; i++) {
                        var x = { text: '班级1', value: 0 };
                        x.text = data[i].practiceTitle;
                        x.value = data[i].id;
                        this.classInfos.push(x);
                    }
                    this.getVideos();
                }
            }).catch(err => {
                console.log(err);
            })
        },
        changeClass(res) {
            console.log(res)
            //根据班级id获取当前的所有任务信息
            for (var i = 0; i < this.classInfos.length; i++) {
                if (this.classInfos[i].value == res) {
                    this.practiceTitle = this.classInfos[i].text;
                    this.getVideos();
                }
            }
        },
    },
    computed: {
    },
    mounted() {
        this.getShixun();
    },
}
</script>